using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace NodeCanvas
{
	[Category("✫ Utility")]
	[ExecuteInEditMode]
	public class ActionList : ActionTask
	{
		public bool runInParallel;

		[SerializeField]
		private List<UnityEngine.Object> actions = new List<UnityEngine.Object>();

		private int currentActionIndex;

		public override float estimatedLength
		{
			get
			{
				float num = 0f;
				foreach (ActionTask item in actions.OfType<ActionTask>())
				{
					num += item.estimatedLength;
				}
				return num;
			}
		}

		protected override string info
		{
			get
			{
				if (actions.Count == 0)
				{
					return "No Actions";
				}
				string text = string.Empty;
				for (int i = 0; i < actions.Count; i++)
				{
					if (!IsTrullyNull(actions[i]))
					{
						ActionTask actionTask = actions[i] as ActionTask;
						if (actionTask == null)
						{
							text = text + MissingTaskText(actions[i]) + "\n";
						}
						else if (actionTask.isActive)
						{
							string text2 = (actionTask.isRunning ? "► " : ((!actionTask.isPaused) ? string.Empty : "<b>||</b> "));
							text = text + text2 + actionTask.summaryInfo + ((i != actions.Count - 1) ? "\n" : string.Empty);
						}
					}
				}
				return text;
			}
		}

		protected override void OnExecute()
		{
			if (actions.Count == 0)
			{
				EndAction();
				return;
			}
			currentActionIndex = 0;
			if (runInParallel)
			{
				for (int i = 0; i < actions.Count; i++)
				{
					ActionTask actionTask = actions[i] as ActionTask;
					if (!actionTask || !actionTask.isActive)
					{
						OnNestedActionEnd(true);
					}
					else
					{
						actionTask.ExecuteAction(base.agent, base.blackboard, OnNestedActionEnd);
					}
				}
			}
			else
			{
				MoveNext();
			}
		}

		private void MoveNext()
		{
			ActionTask actionTask = actions[currentActionIndex] as ActionTask;
			if (!actionTask || !actionTask.isActive)
			{
				OnNestedActionEnd(true);
			}
			else
			{
				actionTask.ExecuteAction(base.agent, base.blackboard, OnNestedActionEnd);
			}
		}

		private void OnNestedActionEnd(ValueType didSucceed)
		{
			if (!(bool)(object)didSucceed)
			{
				EndAction(false);
				return;
			}
			currentActionIndex++;
			if (runInParallel)
			{
				if (currentActionIndex == actions.Count)
				{
					EndAction(true);
				}
			}
			else if (currentActionIndex < actions.Count)
			{
				MoveNext();
			}
			else
			{
				EndAction(true);
			}
		}

		protected override void OnStop()
		{
			foreach (ActionTask item in actions.OfType<ActionTask>())
			{
				item.EndAction(false);
			}
		}

		protected override void OnPause()
		{
			foreach (ActionTask item in actions.OfType<ActionTask>())
			{
				item.PauseAction();
			}
		}

		protected override void OnGizmos()
		{
			foreach (ActionTask item in actions.OfType<ActionTask>())
			{
				item.DrawGizmos();
			}
		}

		protected override void OnGizmosSelected()
		{
			foreach (ActionTask item in actions.OfType<ActionTask>())
			{
				item.DrawGizmosSelected();
			}
		}

		private string MissingTaskText(UnityEngine.Object o)
		{
			string text = ((!object.Equals(o, null)) ? o.ToString() : "NULL ENTRY");
			text = text.Replace(base.gameObject.name + " ", string.Empty);
			return string.Format("<color=#ff6457>* {0} *</color>", text);
		}

		private bool IsTrullyNull(UnityEngine.Object o)
		{
			try
			{
				return o == null && o.GetType() != typeof(UnityEngine.Object);
			}
			catch
			{
				return true;
			}
		}
	}
}
